Online
Full Software
News Plus
Contents
Trial Software
Hands On
Essential Files
Contact Details
Help Section
Contents Page



C++ Builder Tutorial

Continuing his C++ tutorial with Borland's C++ Builder, Dermot Hogan looks at one of it's key features รป classes.

Object orientation didn't start with C++, but it's become by far the most popular language to write object-oriented programs in. With C++ Builder, you'll find that you can combine the rapid application style of development common to Delphi and Borland with the power and flexibility of C++.

This month, we show you how to use C++ classes and objects in a simple but realistic example of object-oriented programming using a doubly linked list. Lists are commonly used to hold collections of objects or keep track of memory. They are widely used within the Windows operating system itself to identify which resources,for example, belong to a particular program.

The C++ tutorial starts with a basic C++ Builder form with just two buttons, one for adding list elements, the other for deleting them from the list. The number of elements will be displayed in a C++ Builder Memo box. You can examine the basic structure of the project in the STEP1 folder.


To Copy the Step1 Folder to your disk.

The next step is to add the class module that deals with constructing the list.

It's good C++ programming practice to hold class definitions and code in separate files (units in C++ Builder terminology). Move on to the next part of the project by running C++ Builder and opening STEP2\PROJECT1.MAK.


To Copy the Step2 Folder to your disk.

Open the Project Manager by selecting View, Project Manager and click on the XLIST.CPP unit. You'll see the code for a doubly linked list. You can view the header file (again according to C++ convention in a separate file) by right-clicking with the mouse somewhere in XLIST.CPP, and selecting Open Source/Header File. This will bring up a new window, XLIST.H, which has the C++ class definition of the Xlist class.

This isn't very complicated, with just two data elements, one for the forward link, the other for the backward-pointing link, and four methods; one that adds a new link; another that removes a link and the third counting the number of elements in the list. You'll see that the form of the fourth one is slightly different from the other three, it's the 'constructor' for the class which is called when a new object of the Xlist class is created.

You can try out the completed program by running STEP3\PROJECT1.EXE.


To Copy the Step3 Folder to your disk.

Add new members to the list by clicking the Add button, and deleting members from the list by using the Remove button. The number of elements in the queue is displayed in the Memo box window.





 
 
Contents | Top | Help